package com.example.sls.dao.logistics;

import com.example.sls.pojo.logistics.EquipmentRentals;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 设备租借记录表(EquipmentRentals)表数据库访问层
 *
 * @author makejava
 * @since 2024-06-24 08:24:24
 */
public interface EquipmentRentalsDao {
  /**
   * 通过ID查询单条数据
   *
   * @param id 主键
   * @return 实例对象
   */
  @Select("select * from equipment_rentals where id=#{id}")
  EquipmentRentals queryById(Integer id);

  /**
   * 查询指定行数据
   *
   * @param equipmentRentals 查询条件
   * @param pageSize         分页对象
   * @param offset           偏移量，起始处
   * @return 对象列表
   */
  @Select("select * from equipment_rentals where 1=1" +
          " and (id=#{equipmentRentals.id} or (isnull(#{equipmentRentals.id}) or #{equipmentRentals.id}=0))" +
          " and (equipment_id=#{equipmentRentals.equipmentId} or (isnull(#{equipmentRentals.equipmentId}) or #{equipmentRentals.equipmentId}=0))" +
          " and (user_id=#{equipmentRentals.userId} or (isnull(#{equipmentRentals.userId}) or #{equipmentRentals.userId}=0))" +
          " and (rental_date=#{equipmentRentals.rentalDate} or (isnull(#{equipmentRentals.rentalDate}) or #{equipmentRentals.rentalDate}=''))" +
          " and (return_date=#{equipmentRentals.returnDate} or (isnull(#{equipmentRentals.returnDate}) or #{equipmentRentals.returnDate}=''))" +
          " limit #{pageSize} offset #{offset}")
  List<EquipmentRentals> queryAllByLimit(EquipmentRentals equipmentRentals, Integer pageSize, Integer offset);

  /**
   * 统计总行数
   *
   * @return 总行数
   */
  @Select("select count(*) from equipment_rentals")
  long count();

  /**
   * 新增数据
   *
   * @param equipmentRentals 实例对象
   * @return 影响行数
   */
  @Insert("insert into equipment_rentals (id,equipment_id,user_id,rental_date,return_date) " +
          "values(" +
          "if(#{id}!=0 and !isnull(#{id}),#{id},null)," +
          "if(!isnull(#{equipmentId}),#{equipmentId},null)," +
          "if(!isnull(#{userId}),#{userId},null)," +
          "if(!isnull(#{rentalDate}),#{rentalDate},null)," +
          "if(!isnull(#{returnDate}),#{returnDate},null)" +
          ")")
  int insert(EquipmentRentals equipmentRentals);

  /**
   * 通过id修改数据
   * id 主键
   *
   * @param equipmentRentals 实例对象
   * @return 影响行数
   */
  @Update("update equipment_rentals set " +
          "equipment_id=if(isnull(#{equipmentId}),equipment_id,#{equipmentId})," +
          "user_id=if(isnull(#{userId}),user_id,#{userId})," +
          "rental_date=if(isnull(#{rentalDate}),rental_date,#{rentalDate})," +
          "return_date=if(isnull(#{returnDate}),return_date,#{returnDate})" +
          " where id=#{id}")
  int update(EquipmentRentals equipmentRentals);

  /**
   * 通过主键删除数据
   *
   * @param id 主键
   * @return 影响行数
   */
  @Delete("delete from equipment_rentals where id=#{id}")
  int deleteById(Integer id);
}

